<?php
namespace app\admin\controller;


use think\Config;
use think\Controller;
use think\captcha\Captcha;
use think\Request;
use think\Session;


class Login extends Controller {

    /**
     * 登陆
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login()
    {
        if($this->request->isPost()){
            $param = $this->request->param();
            $captcha = new Captcha();
            if( !$captcha->check($param['code']))
            {
                return json(array('code'=>0,'msg'=>'验证码错误'));
            }
            $usereInfo = Db::name('admin_user')->field('admin_id,admin_username,admin_pwd')->where(['admin_username'=>$param['username']])->find();

            if($param['username'] == $usereInfo['admin_username'] && md5($param['password']) == $usereInfo['admin_pwd']){
                Session('uid',$usereInfo['admin_id']);
                return json(array('code'=>1,'msg'=>'登陆成功'));
            }else{
                return json(array('code'=>0,'msg'=>'用户名或密码错误'));
            }
        }

        return $this->fetch();
    }

    /**
     * 验证码
     * @return \think\Response
     */
    public function verify()
    {

        $captcha = new Captcha(config('captcha.captcha'));
        return $captcha->entry();
    }


}